const match = require("minimatch");
const evaluate = require("./evaluate");

module.exports = function filter(filters, files, data, done) {
  // 文件匹配
  const fileNames = Object.keys(files);
  Object.keys(filters).forEach((glob) => {
    fileNames.forEach((file) => {
      // 匹配文件是否相同,dot为true是为了匹配/**/**
      if (match(file, glob, { dot: true })) {
        const conditions = filters[glob];
        if (!evaluate(conditions, data)) {
          // 文件删除
          delete files[file];
        }
      }
    });
  });
  done();
};
